// pages/mine-zz2/mine-zz2.js
const re = require("../../utils/request.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activityId: "",
    activityName: "",
    activityCode: "",
    activityUrl: "",
    info: {}
  },
  toScan() {
    const activityId = this.data.activityId
    const activityCode = this.data.activityCode
    wx.scanCode({
      success(res) {
        const studentId = res.result
        if (studentId) {
          // 提交活动
          const data = {
            activityId: activityId,
            studentId: studentId,
            code: activityCode
          }
          console.log(data)
          re.postRequest('/score-record/method1-2', data).then(res => {
            if (res.msg) {
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 1000
              })
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const url = `${re.serverUrl}/code/activity/${options.id}`
    this.setData({
      activityId: options.id,
      activityName: options.name,
      activityUrl: url
    })

    // 获取活动code
    re.getRequest(`/code/activity/text/${options.id}`).then(res => {
      const activityCode = res.data
      this.setData({
        activityCode: activityCode
      })
    })

    // 获取活动详细信息
    re.getRequest(`/activity/detail/${options.id}`).then(res => {
      let data = res.data
      // 处理日期格式、图片格式
      data.startTime = data.startTime.replace("T", " ")
      data.endTime = data.endTime.replace("T", " ")
      data.poster = JSON.parse(data.poster)
      data.poster = data.poster.map(item => re.serverUrl + "/img/" + item)
      this.setData({
        info: data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})